package cn.edu.gzist.chapter03.config;

import cn.edu.gzist.chapter03.intercepter.LoginIntercepter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig  implements WebMvcConfigurer {
// spring.mvc.static-path-pattern=/backend/**
// spring.web.resources.static-locations=classpath:/backend/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/backend/**")
                .addResourceLocations("classpath:/backend/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("/backend/index.html");
        registry.addViewController("/login").setViewName("/backend/login.html");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(new LoginIntercepter())
               .addPathPatterns("/**")   // 拦截地址
               .excludePathPatterns("/backend/login.html","/login"); // 排除地址
    }
}
